<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色分配菜单</title>
    <link rel="stylesheet" href="../../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" action="" style="padding:20px;">
            <input type="hidden" name="roleId" value=""/>
            <div class="layui-form-item">
                <label class="layui-form-label required">角色名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required" lay-reqtext="角色名称不能为空" placeholder="请输入角色名称" value="" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择菜单</label>
                <div class="layui-input-inline">
                    <div id="treeMenu" class="demo-tree-more"></div>
                </div>
                <div class="layui-form-mid layui-word-aux">如果不选择，默认是清空用户之前拥有的菜单</div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="../../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    function getMenuIds(data = [], arr = []) {
        for (let item of data) {
            arr.push(item.id);
            if (item.children && item.children.length) {
                getMenuIds(item.children, arr);
            }
        }
        return arr;
    }
    layui.use(['table', 'form', 'tree'], function () {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            tree = layui.tree;

        $.ajax({
            url:"../../../sys/menu/queryTreeMenus",
            type:"get",
            dataType:"json",
            success:function(result){
                if(result.code==0){
                    var data = result.data;
                    //渲染菜单树
                    tree.render({
                        elem: '#treeMenu'
                        ,data: data
                        ,showCheckbox: true  //是否显示复选框
                        ,id: 'treeMenu'
                        ,spread: true

                        ,isJump: true //是否允许点击节点时弹出新窗口跳转
                        ,click: function(obj){
                            var data = obj.data;  //获取当前点击的节点数据
                            layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                        }
                    });
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = data.field;
            //获取选中节点的数据
            var checkedData = tree.getChecked('treeMenu');
            //获取选中的所有菜单id
            var menuIds = getMenuIds(checkedData);
            data.menuIds = menuIds.join();
            $.ajax({
                url:"../../../sys/role-menu/add",
                type:"post",
                data:data,
                dataType:"json",
                success:function(result){
                    layer.msg(result.msg);
                    if(result.code==0){
                        //关闭窗口
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>